/**
 * 
 */
package org.store.book.pages;

import java.text.NumberFormat;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.store.book.model.Book;

/**
 * @author Uday Shankar
 * 
 */
public class Index extends BSBasePage {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 */
	public Index() {
		this(null);
	}

	/**
	 * @param parameters
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Index(PageParameters parameters) {
		super(parameters);

		add(new ListView<Book>("books", getBookList()) {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<Book> item) {

				Book book = (Book) item.getModelObject();

				item.add(new Label("bookName", book.getBookName()));
				item.add(new Label("author", book.getAuthor()));
				item.add(new Label("price", book.getPrice()));

				item.add(new Link<Book>("add", item.getModel()) {

					/**
					 * 
					 */
					private static final long serialVersionUID = 1L;

					public void onClick() {
						Book selectedBook = (Book) getModelObject();
						getCart().getListOfBooks().add(selectedBook);
					}

				});

			}

		});

		add(new ListView<Book>("cart", new PropertyModel(this,
				"cart.listOfBooks")) {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<Book> item) {
				Book book = (Book) item.getModelObject();

				item.add(new Label("name", book.getBookName()));
				item.add(new Label("price", "Rs. " + book.getPrice()));
				item.add(new Link("remove", item.getModel()) {

					/**
					 * 
					 */
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick() {
						Book unselectedBook = (Book) getModelObject();
						getCart().getListOfBooks().remove(unselectedBook);
					}

				});
			}
		});
		add(new Label("total", new Model() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public String getObject() {
				NumberFormat nf = NumberFormat.getCurrencyInstance();
				double total = getCart().getTotal();
				getCart().setTotalBillValue(total);
				return nf.format(total);
			}
		}));
		add(new Link("checkout"){

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			
			@Override
			public void onClick() {
				setResponsePage(new CheckOut(null,getBSSession().getCart()));
				
			}
			
			public boolean isVisible(){
				return !getCart().getListOfBooks().isEmpty();
			}
			
		});
	}
}
